
package com.naza.rpc.netty;

import com.naza.rpc.serialize.RpcSerializeProtocol;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

import java.util.Map;

/**
 * RPC服务端管理初始化 实现服务端消息编码、解码、处理的核心模块
 * 
 * @author yl
 */
public class RpcReceiveChannelInitializer extends ChannelInitializer<SocketChannel> {

    private RpcSerializeProtocol protocol;

    private RpcRecvSerializeFrame frame = null;

    RpcReceiveChannelInitializer buildRpcSerializeProtocol(RpcSerializeProtocol protocol) {
        this.protocol = protocol;
        return this;
    }

    RpcReceiveChannelInitializer(Map<String, Object> handlerMap) {
        frame = new RpcRecvSerializeFrame(handlerMap);
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        frame.select(protocol, pipeline);
    }
}
